/**
 *  @author Cornelius Huang
 *  capability manager module
 *  
 *  hangzhourecruitment.fssbu@capgemini.com
 */
app.controller('mailTabController', ['$scope', '$uibModal', '$q', '$state', 'cmSlideCacheService', 'dateFilter', 'slideEmailService', 'upload', 'tsuikaService', function($scope, $uibModal, $q, $state, cmSlideCacheService, dateFilter, slideEmailService, upload, tsuikaService) {
	$scope.mail = {
		to: 'hangzhourecruitment.fssbu@capgemini.com',
		cc: null,
		attachPath: null,
		attachName: null,
		opnDate: null,
		capa: null,
		posTit: null,
		desDsg: null,
		hirRes: null,
		hcReq: null,
		billDate: null,
		wrkLoc: null,
		cusInter: null,
		projIntr: null,
		duty: null,
		other: null,
		teches: [],
		langs: [],
		isflp: null,
		refName: null,
		primarySkill: null,
		secondarySkill: null,
		language: null,
		targetJoinDate: null,
		demandShowId: null
	};
	
	var cmDmd = angular.copy(cmSlideCacheService.cmCache);
	console.log(cmDmd);
	$scope.attachFile = null;
	
	if(cmDmd.dmdShowID == undefined && cmDmd.dmdShowID == null) {
		$state.go('capa.tbd');
	}
	
	$scope.mail.demandShowId = cmDmd.dmdShowID;
	$scope.mail.opnDate = dateFilter(cmDmd.startDate, 'yyyy-MM-dd');
	$scope.mail.capa = cmDmd.capability;
	$scope.mail.desDsg = cmDmd.desigation;
	$scope.mail.billDate = dateFilter(cmDmd.startDate, 'yyyy-MM-dd');
	$scope.mail.wrkLoc = cmDmd.site;
	
	$scope.$watch('mail.demandShowId', function(newValue, oldValue) {
		console.log(newValue);
		tsuikaService.getResult(newValue).then(function(result) {
			console.log(result);
			if(result != '') {
				$scope.mail = {
						to: 'hangzhourecruitment.fssbu@capgemini.com',
						cc: result.cc,
						attachPath: result.attachPath,
						attachName: result.attachName,
						opnDate: result.openDate,
						capa: null,
						posTit: null,
						desDsg: null,
						hirRes: result.hiringReason,
						hcReq: result.hcRequired,
						billDate: result.billingDate,
						wrkLoc: result.workLoacation,
						cusInter: result.custInterview,
						projIntr: result.projectIntro,
						duty: result.duties,
						other: result.other,
						teches: result.technicalSkills,
						langs: result.languageSkills,
						isflp: result.isflp,
						refName: result.refName,
						primarySkill: result.primarySkill,
						secondarySkill: result.secondarySkill,
						language: result.language,
						targetJoinDate: result.targetJoinDate,
						demandShowId: result.demandShowId
					};
			}
		})		
	});
			
	$scope.$watch('mail.teches.length', function(newValue, oldValue) {
		$scope.mail.secondarySkill = '';
		var length = $scope.mail.teches.length;
		for(var i = 0; i < length; i++) {
			var techskill = $scope.mail.teches[i].techSkil;
			if(i !== length - 1)
				techskill = techskill + ', ';
			$scope.mail.secondarySkill = $scope.mail.secondarySkill.concat(techskill);
		}
		
	});
	
	$scope.$watch('mail.langs.length', function(newValue, oldValue) {
		$scope.mail.language = '';
		var length = $scope.mail.langs.length;
		for(var i = 0; i < length; i++) {
			var langname = $scope.mail.langs[i].langSkill;
			if(i !== length - 1)
				langname = langname + ', ';
			$scope.mail.language = $scope.mail.language.concat(langname);
		}
	});
	
	$scope.send = function() {
		console.log($scope.mail);
		console.log(JSON.stringify($scope.mail));
		var file = $scope.attachFile;
		
		if(file !== null) {
			$scope.fileES6Http(file).then(function(response) {
				var result = eval(response);
				$scope.mail.attachPath = result[1];
				$scope.mail.attachName = result[0];
								
				slideEmailService($scope.mail).then(function(result) {
					console.log(result);
					
					var modalInstance = $uibModal.open({
						animation: false,
						size: 'sm',
						templateUrl: './views/coralert.html',
						controller: 'alertController',
						resolve: {
							item: function() {
								return 'Email Sending SUCCESS.';
							}
						}
					});
					
					modalInstance.result.then(function() {
						//slideCallbackService.promiseDeferd.notify('promise noitfy');
						$state.go('capa.tbd');
					}, function() {
						//slideCallbackService.promiseDeferd.notify('promise noitfy');
						$state.go('capa.tbd');
					});
				}, function(result) {
					console.log(result);
				});
				
			}, function(response) {
				console.log(response);
			});
		} else {
			console.log(JSON.stringify($scope.mail));
			slideEmailService($scope.mail).then(function(result) {
				console.log(result);
				
				var modalInstance = $uibModal.open({
					animation: false,
					size: 'sm',
					templateUrl: './views/coralert.html',
					controller: 'alertController',
					resolve: {
						item: function() {
							return 'Email Sending SUCCESS.';
						}
					}
				});
				
				modalInstance.result.then(function() {
					//slideCallbackService.promiseDeferd.notify('promise noitfy');
					$state.go('^');
				}, function() {
					//slideCallbackService.promiseDeferd.notify('promise noitfy');
					$state.go('^');
				});
			}, function(result) {
				console.log(result);
			});
		}
		
	}
		
	$scope.fileES6Http = function(file) {
		var http = new XMLHttpRequest();
		var formdata = new FormData();
		var promise = $q(function(resolve, reject) {
			formdata.append(file[0].name, file[0]);
			http.open('POST', '/DRMS/api/drmsEmail/fileUpload');
			http.send(formdata);
			
			http.onreadystatechange = function() {
				if(http.readyState === XMLHttpRequest.DONE) {
					if(http.status === 200) {
						resolve(http.responseText);
					} else {
						reject('doAjaxRequest - fail');
					}
				}
			}
		});
		return promise;
	} 
	
	$scope.getFile = function(file) {
		console.log(file[0]);
		if(file.length === 0) {
			$scope.attachFile = null;
		} else {
			$scope.attachFile = file;
		}
		/*$scope.fileES6Http(file).then(function(response) {
			console.log(response);
		}, function(response) {
			console.log(response);
		});*/				
		//$scope.fileHttp($scope.attachFile);
	}
	
	$scope.$watch('attachFile', function(newValue, oldValue) {
		console.log(newValue);
	})
}]);

app.controller('resTabController', ['$scope', '$state', '$uibModal', 'resFullService', 'capabilityService', 'cmSlideCacheService', 'resFillService', 'slideCallbackService', function($scope, $state, $uibModal, resFullService, capabilityService, cmSlideCacheService, resFillService, slideCallbackService) {
	
	$scope.name = '';
	$scope.resources;
	
	$scope.resourceId = null;
	$scope.assCapId = null;
		
	console.log(cmSlideCacheService.cmCache);
	
	
	
	$scope.$watch('name', function(newValue, oldValue) {
		//console.log('Name watch - ' + newValue);
		
		resFullService.searFullName(newValue).then(function(result) {
			console.log(result[0]);
			$scope.resources = result;
		}, function(result) {
			console.log(result)
		})
		
		/*resSearService(newValue).then(function(result) {
			$scope.resources = result;
		}, function(result) {
			console.log(result);
		});*/
	})
	
	$scope.$watch('resources.length', function(newValue, oldValue) {
		if(newValue == 1) {
			$scope.resourceId = $scope.resources[0].resourceId;
			//TODO: set assgin capbality
			$scope.capa = $scope.resources[0].capability;
		} else {
			$scope.resourceId = null;
		}
	});
	
	$scope.$watch('resourceId', function(newValue, oldValue) {
		console.log('Request resourceId: ' + newValue);
	});
	
	$scope.$watch('assCap', function(newValue, oldValue) {
		if(newValue != undefined) {
			$scope.assCapId = newValue.capId;
		}
	});
	
	$scope.confirm = function() {
		if($scope.resourceId != null && $scope.assCapId != null) {
			resFillService(cmSlideCacheService.cmCache.dmdID, $scope.resourceId, $scope.assCapId).then(function(result) {
				console.log(result);
				var modalInstance = $uibModal.open({
					animation: false,
					size: 'sm',
					templateUrl: './views/coralert.html',
					controller: 'alertController',
					resolve: {
						item: function() {
							return 'result filling SUCCESS.';
						}
					}
				});
				
				modalInstance.result.then(function() {
					slideCallbackService.promiseDeferd.notify('promise noitfy');
					$state.go('^');
				}, function() {
					slideCallbackService.promiseDeferd.notify('promise noitfy');
					$state.go('^');
				});
			}, function(result) {
				
			});
		}
	}
	
	$scope.iptBlur = function() {
		if($scope.resources.length == 1) {
			$scope.name = $scope.resources[0].name;
		} else {
				
		}
	}
	$scope.matching = function(id, name) {
		$scope.name = name;
		$scope.resourceId = id;
	}
}]);

app.controller('DetailTabController', ['$scope', '$state', 'cmSlideCacheService', 'dateFilter', function($scope, $state, cmSlideCacheService, dateFilter) {
	console.log(cmSlideCacheService);
	if(cmSlideCacheService.cmCache == undefined || cmSlideCacheService.cmCache == null) {
		$state.go('capa.tbd');
	}
	$scope.dmdDetail = cmSlideCacheService.cmCache;
	$scope.dmdDetail.startDate = dateFilter($scope.dmdDetail.startDate, 'dd-MM-yyyy');
	$scope.dmdDetail.endDate = dateFilter($scope.dmdDetail.endDate, 'dd-MM-yyyy');
}]);

app.controller('CMController', ['$scope', '$q', '$state', '$window', 'cmDemandService', 'cmSlideCacheService', 'pagingFilter', 'filterFilter', function($scope, $q, $state, $window, cmDemandService, cmSlideCacheService, pagingFilter, filterFilter) {
	
	$scope.currPage = 1;
	$scope.totalItems;
	$scope.itemsPer = 15;
	
	$scope.capa1;
	$scope.capa2;
	
	var names = ['capa1', 'capa2'];
	
	if($window.innerHeight < 800) {
		$scope.itemsPer = 10;
	}
	
	var codRequest = function(capa, status) {
		//TODO: None to Active
		if(status != undefined && status.dmdStatusName == 'Active') {
			status = undefined;
		}
		//TODO: None to Active
		if(capa != undefined && capa.capName == 'None') {
			capa = undefined;
		} 
		var promise = $q(function(resolve, reject) {
			if(capa != undefined && status != undefined) {
				cmDemandService(capa.capName, status.dmdStatusName).then(function(result) {
					reqResult = result;
					resolve(reqResult);
				}, function(result) {
					reject('codRequest - reject');
				});
			} else if(capa != undefined && status == undefined){
				cmDemandService(capa.capName, '').then(function(result) {
					reqResult = result;
					resolve(reqResult);
				}, function(result) {
					reject('codRequest - reject');
				});
			} else if(capa == undefined && status != undefined) {
				cmDemandService('', status.dmdStatusName).then(function(result) {
					reqResult = result;
					resolve(reqResult);
				}, function(result) {
					reject('codRequest - reject');
				});
			} else {
				cmDemandService('', '').then(function(result) {
					reqResult = result;
					resolve(reqResult);
				}, function(result) {
					reject('codRequest - reject');
				});
			}
		});
		return promise;
	}
	
	$scope.doTask = function(disDmd, $event) {
		cmSlideCacheService.cmCache = disDmd;
		$state.go('capa.tbd.twom');
		$event.stopPropagation();
		$event.preventDefault();
	}
	
	$scope.$watchGroup(names, function(newNames, oldNames) {
		console.log(newNames);
		codRequest(newNames[0], newNames[1]).then(function(result) {
			var reqresult = filterFilter(result, $scope.keyword);
			pagingFilter.listLoad(reqresult, $scope.itemsPer);
			$scope.totalItems = pagingFilter.getPgInfo().resArylength;
			$scope.s = pagingFilter.getCurrentPage($scope.currPage);
		});
	});

	$scope.$watch('keyword', function(newValue, oldValue) {
		codRequest($scope.capa1, $scope.capa2).then(function(result) {
			var reqresult = filterFilter(result, newValue);
			pagingFilter.listLoad(reqresult, $scope.itemsPer);
			$scope.totalItems = pagingFilter.getPgInfo().resArylength;
			$scope.disDmds = pagingFilter.getCurrentPage($scope.currPage);
		});
	});
	
	$scope.$watch('currPage', function(newValue, oldValue) {
		$scope.disDmds = pagingFilter.getCurrentPage(newValue);
	});
	
}]);